package com.hanma.dao.impl;

import com.hanma.bean.Category;
import com.hanma.bean.Comments;
import com.hanma.dao.CommentsDao;
import com.hanma.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CommentsDaoImpl implements CommentsDao {
    @Override
    public int insert(Comments comments) {
        Connection conn = DBUtil.open();
        int num=0;
        String sql="insert into comments(uid,nid,nickname,comment,img)  values(?,?,?,?,?)";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, comments.getUid());
            ps.setInt(2, comments.getNid());
            ps.setString(3, comments.getNickname());
            ps.setString(4, comments.getComment());
            ps.setString(5, comments.getImg());
            num=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(null, ps, conn);
        }
        return num;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public int update(Comments comments) {
        return 0;
    }

    @Override
    public Comments find(Integer id) {
        return null;
    }

    @Override
    public List<Comments> findAll(Integer nid) {
        Connection conn = DBUtil.open();
        String sql="select * from comments where nid=? order by id desc";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Comments> commentsList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, nid);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer uid =rs.getInt("uid");
                String nickname=rs.getString("nickname");
                String comment=rs.getString("comment");
                String img = rs.getString("img");
                Comments comments = new Comments(uid,nid,nickname,comment,img);
                commentsList.add(comments);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return commentsList;
    }

    @Override
    public List<Comments> findAllUser(Integer uid) {
        Connection conn = DBUtil.open();
        String sql="select * from comments where uid=? order by id desc";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Comments> commentsList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer nid =rs.getInt("uid");
                String nickname=rs.getString("nickname");
                String comment=rs.getString("comment");
                Comments comments = new Comments(uid,nid,nickname,comment);
                commentsList.add(comments);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return commentsList;
    }

    @Override
    public List<Comments> findAllUser(Integer uid, Integer start, Integer size) {
        Connection conn = DBUtil.open();
        String sql="select * from comments where uid=? order by id desc limit ?,?";
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Comments> commentsList = new ArrayList<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, uid);
            ps.setInt(2, start);
            ps.setInt(3, size);
            rs = ps.executeQuery();
            while(rs.next()) {
                Integer nid =rs.getInt("uid");
                String nickname=rs.getString("nickname");
                String comment=rs.getString("comment");
                String img = rs.getString("img");
                Comments comments = new Comments(uid,nid,nickname,comment,img);
                commentsList.add(comments);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(rs, ps, conn);
        }
        return commentsList;
    }

    @Override
    public int updateHead(Integer uid, String img) {
        Connection conn = DBUtil.open();
        int num=0;
        String sql="update comments set img=? where uid=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, img);
            ps.setInt(2, uid);
            num=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(null, ps, conn);
        }
        return num;
    }

    @Override
    public int updateNickname(Integer uid, String nickname) {
        Connection conn = DBUtil.open();
        int num=0;
        String sql="update comments set nickname=? where uid=?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, nickname);
            ps.setInt(2, uid);
            num=ps.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(null, ps, conn);
        }
        return num;
    }
}
